using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ACME.Northwind.DataTransfer;
using System.Linq.Expressions;
using ACME.Northwind.Service.Interfaces;

namespace ACME.Northwind.WCFProxy
{
	/// <summary>
	/// The service implementation for Employee object
	/// </summary>
	public partial class EmployeeService : IEmployeeService
	{
		#region IEmployeeService Members

		/// <summary>
		/// Run a selection by the primary key
		/// </summary>
		public EmployeeDTO SelectByPrimaryKey(int employeeid)
		{
			return DataService.Instance.GetEmployee(employeeid);
		}

		/// <summary>
		/// Run the persist for a list
		/// </summary>
		public bool Persist(List<EmployeeDTO> list)
		{
			bool retval = true;
			foreach (EmployeeDTO employee in list)
			{
				retval &= this.Persist(employee);
			}
			return retval;
		}

		/// <summary>
		/// Run the persist for an object
		/// </summary>
		public bool Persist(EmployeeDTO dto)
		{
			return (DataService.Instance.PersistEmployee(dto) != null);
		}

		/// <summary>
		/// Run a selection using LINQ
		/// </summary>
		public List<EmployeeDTO> RunSelect(string linq)
		{
			List<EmployeeDTO> retVal = new List<EmployeeDTO>();
			retVal.AddRange(DataService.Instance.GetEmployeeListByLINQ(linq));
			return retVal;
		}

		/// <summary>
		/// Run a paginated selection using LINQ
		/// </summary>
		public PagedQueryResults<EmployeeDTO> RunSelect(int pageIndex, int pageSize, bool ascending, string sortColumn, string linq)
		{
			return DataService.Instance.GetEmployeeListPaged(pageIndex, pageSize, ascending, sortColumn, linq);
		}

		/// <summary>
		/// Run a selection for all objects
		/// </summary>
		public List<EmployeeDTO> RunSelect()
		{
			List<EmployeeDTO> retVal = new List<EmployeeDTO>();
			DataServiceClient ds = DataService.Instance;
			retVal.AddRange(ds.GetEmployeeList());
			return retVal;
		}

		/// <summary>
		/// Return a list of related Employee objects
		/// </summary>
		public List<EmployeeDTO> GetEmployeeList(EmployeeDTO item)
		{
			List<EmployeeDTO> retval = new List<EmployeeDTO>();
			DataServiceClient ds = DataService.Instance;
			string s = "";
			s += "x.ReportsTo = " + item.EmployeeId + "";
			retval.AddRange(ds.GetEmployeeListByLINQ("x => " + s));
			return retval;
		}

		/// <summary>
		/// Return a list of related EmployeeTerritory objects
		/// </summary>
		public List<EmployeeTerritoryDTO> GetEmployeeTerritoryList(EmployeeDTO item)
		{
			List<EmployeeTerritoryDTO> retval = new List<EmployeeTerritoryDTO>();
			DataServiceClient ds = DataService.Instance;
			string s = "";
			s += "x.EmployeeId = " + item.EmployeeId + "";
			retval.AddRange(ds.GetEmployeeTerritoryListByLINQ("x => " + s));
			return retval;
		}

		/// <summary>
		/// Return a list of related Order objects
		/// </summary>
		public List<OrderDTO> GetOrderList(EmployeeDTO item)
		{
			List<OrderDTO> retval = new List<OrderDTO>();
			DataServiceClient ds = DataService.Instance;
			string s = "";
			s += "x.EmployeeId = " + item.EmployeeId + "";
			retval.AddRange(ds.GetOrderListByLINQ("x => " + s));
			return retval;
		}

		/// <summary>
		/// Return the related Employee item
		/// </summary>
		public EmployeeDTO GetEmployeeItem(EmployeeDTO item)
		{
			EmployeeDTO retval = new EmployeeDTO();
			if (false ||
				(item.ReportsTo == null) ||
				false
			)
			{
				//The primary key cannot be null, so there is no object
				return null;
			}
			DataServiceClient ds = DataService.Instance;
			retval = ds.GetEmployee(item.ReportsTo.Value);
			return retval;
		}

		/// <summary>
		/// Delete the specified item
		/// </summary>
		public bool Delete(EmployeeDTO item)
		{
			DataServiceClient ds = DataService.Instance;
			return ds.DeleteEmployee(item);
		}

		#endregion
	}
}
